/**
 * 
 */
package com.acc.softmgr.common.genertor.gen;

import java.sql.SQLException;
import java.util.List;

import org.apache.velocity.VelocityContext;

import com.acc.softmgr.common.genertor.bean.ColumnData;
import com.acc.softmgr.common.genertor.utils.CommUtil;
import com.acc.softmgr.common.genertor.utils.PageParserUtil;

/**
 * 
 * @content 生成HTML页面
 * 
 * @date 2017年6月3日
 * 
 * @author OprCalf
 * 
 */
public class GenerateHTML extends BaseGenerate {

	public static void genHTML(String tableName, String baseHTMLPath, String htmlPath) throws SQLException {
		String className = CommUtil.formatTableNameToClassName(tableName);
		String classNameLower = className.substring(0, 1).toLowerCase() + className.substring(1, className.length());

		List<ColumnData> dataList = createBean.getColumnDatas(tableName);

		VelocityContext context = new VelocityContext();
		context.put("titleName", classNameLower + "管理");
		context.put("className", className);
		context.put("classNameLower", classNameLower);
		context.put("initSearchAndEditInfo", initSearchAndEditInfo(dataList));
		context.put("initListInfo", initListInfo(dataList));

		PageParserUtil.WriterPage(context, "HTMLTemplate.ftl", baseHTMLPath, htmlPath);
	}

	// 初始化查询信息跟编辑信息
	private static String initSearchAndEditInfo(List<ColumnData> dataList) {
		StringBuffer str = new StringBuffer();
		if (dataList.size() == 1) {
			str.append("\r\t");
			str.append("<tr><td>" + dataList.get(0).getFormatColumnName() + "：</td><td><input name=\""
					+ dataList.get(0).getFormatColumnName() + "\" id=\"" + dataList.get(0).getFormatColumnName()
					+ "\" type=\"text\" class=\"easyui-validatebox txt\" /></td></tr>");
			str.append("\r\t");
		} else {
			str.append("<tr>");
			str.append("\r\t");
			for (int i = 0; i < dataList.size(); i++) {
				str.append("<td>" + dataList.get(i).getFormatColumnName() + "：</td>");
				str.append("\r\t");
				str.append("<td><input name=\"" + dataList.get(i).getFormatColumnName() + "\" id=\""
						+ dataList.get(i).getFormatColumnName()
						+ "\" type=\"text\" class=\"easyui-validatebox txt\" /></td>");
				str.append("\r\t");
				if ((i + 1) % 2 == 0) {
					str.append("</tr>");
					str.append("\r\t");
					str.append("<tr>");
					str.append("\r\t");
				}
			}
			str.append("</tr>");
			str.append("\r\t");
		}

		return str.toString();
	}

	private static String initListInfo(List<ColumnData> dataList) {
		StringBuffer str = new StringBuffer();
		for (ColumnData cd : dataList) {
			if ("PRI".equals(cd.getColumnKey().toUpperCase())) {
				str.append("<th data-options=\"field:'" + cd.getFormatColumnName()
						+ "',width:100,align:'center',hidden:'true'\">主键</th>");
				str.append("\r\t");
			} else {
				str.append("<th data-options=\"field:'" + cd.getFormatColumnName() + "',width:100,align:'center'\">"
						+ cd.getFormatColumnName() + "</th>");
				str.append("\r\t");
			}
		}
		return str.toString();
	}
}
